#!/usr/bin/python
# -*- coding: UTF-8 -*-
# author: WuJian 2022-10-12
# desc: 主测试类定义

import unittest
import sys
from unittestreport import TestRunner

class MainUnitTestApp:
    def __init__(self):
        self.html_enable = True
        self.dingding_enable = False
        pass

    def start(self):
        my_dict = {'gnss':'test_a_gnss.py',
                   'cam':'test_5cam.py',
                   'lsensor':'test_9lsensor.py',
                   'acc':'test_2acc.py',
                   'thermal':'test_6thermal.py',
                   'general':'test_general.py',
                   'btn':'test_3btn.py',
                   'ftp':'test_7ftp.py',
                   'tp':'test_3tp.py',
                   'gsensor':'test_a_gsensor.py',
                   'emmc':'test_4emmc.py',
                   'uac':'test_8uac.py',
                   'all':'test*.py',
                   'selftest':'selftest*.py',
                   'gs':'test_a_*.py'
                   }
        try:
            test_param = sys.argv[1]
        except Exception as e:
            test_param = 'all'
        pattern = my_dict.get(test_param, 'test*.py')
        
        print("run testcase:", test_param)
        print("execute file:", my_dict.get(test_param, 'test*.py'))
        suite = unittest.defaultTestLoader.discover(start_dir=".", pattern=pattern)

        if self.html_enable:
            runner = TestRunner(suite=suite, filename='test_report.html', report_dir='./', title='主单元DriverApi测试报告', tester='吴建', 
                desc="主单元DriverApi测试报告", templates=1)
            runner.run()
            if self.dingding_enable:
                url = "https://oapi.dingtalk.com/robot/send?access_token=6245f6a147c21b4f213db3d30e0b7f2b1e867fd7de37ece91a9990259231bfac"
                # 发送钉钉通知 
                runner.dingtalk_notice(url=url, secret='SEC1c3ff229aa4dd3515b246a1570f6e1f2d6693cc372e674789ffeb37f004ca9fb')
        else:
            runner = unittest.TextTestRunner(verbosity=2)
            runner.run(suite)

if __name__ == "__main__":
    MainUnitTestApp().start()
